package com.hitqz.robot.driver.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class WheelCacheUtil {

    //缓存状态实时值的key
    public static final String KEY_AIR_QUALITY = "airQuality";
    public static final String KEY_RUNNING_LOG = "runningLog";
    public static final String KEY_MAP_2D = "map2D";
    public static final String KEY_MAP_3D = "map3D";
    public static final String KEY_MAP_PB = "mapPB";
    public static final String KEY_MOVE_BASE_GOAL_LIST = "moveBaseGoalList";;
    public static final String KEY_SENSOR_COUNT = "sensorCount";
    public static final String KEY_POWER_INFO = "powerInfo";
    public static final String KEY_RECHARGE_STATUS = "rechargeStatus";
    public static final String KEY_LOC_ENABLE_STATUS = "locateEnableStatus";
    public static final String KEY_EMERGENCY_STATUS = "emergencyStatus";
    public static final String KEY_LOC_MODE = "locateMode";
    public static final String KEY_NAV_MODE = "navMode";
    public static final String KEY_NAV_LIDAR_ENABLE_STATUS = "navLidarEnableStatus";
    public static final String KEY_HAND_MODE = "handMode";
    public static final String KEY_CURR_POSE = "currentPose";
    public static final String KEY_LASER_SCAN = "laserScan";
    public static final String KEY_SENSOR_ENABLE_STATUS = "sensorEnableStatus";
    public static final String KEY_SENSOR_STATUS = "sensorStatus";
    public static final String KEY_MAP_2D_NAMES = "map2DNames";
    public static final String KEY_MAP_3D_NAMES = "map3DNames";
    public static final String KEY_MAP_PB_NAMES = "mapPBNames";
    public static final String KEY_MP3_NAMES = "mp3Names";
    public static final String KEY_CURR_MAP_NAME = "currMapName";
    public static final String KEY_TASK_QUEUE_STATUS = "taskQueueStatus";
    public static final String KEY_SPEED = "speed";
    public static final String KEY_HEART_BEAT = "heartBeat";
    public static final String KEY_POSITION_STATUS = "positionStatus";


    private static final Cache<String, Boolean> speedFlagCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(800, TimeUnit.MILLISECONDS) // 缓存过期时间
            .build();

    private static final Cache<String, Object> cache = CacheBuilder.newBuilder()
            .maximumSize(60)// fixme 缓存最大容量,按key实际种类去扩容
            .expireAfterWrite(5, TimeUnit.SECONDS) // 缓存过期时间
            .build();

    public static void setSpeedFlag(){
        speedFlagCache.put("speed", true);
    }

    public static boolean getSpeedFlag(){
        return Optional.ofNullable( speedFlagCache.getIfPresent("speed")).orElse(false);
    }

    // 使用命令 号做 key
    public static void put(String key, Object value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        return cache.getIfPresent(key);
    }


}
